package com.xxl.job.admin.controller;

import com.xxl.job.admin.controller.annotation.PermissionLimit;
import com.xxl.job.admin.dao.DbInfoDao;
import com.xxl.job.admin.dao.XxlJobGroupDao;
import com.xxl.job.admin.service.DbInfoService;
import com.xxl.job.core.biz.model.DbInfo;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.enums.DbinfoBelongEnum;
import com.xxl.job.core.util.DateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dbinfo")
public class DbInfoController {

    @Resource
    private DbInfoDao dbInfoDao;

    @Resource
    private DbInfoService dbInfoService;

    @Resource
    private XxlJobGroupDao xxlJobGroupDao;

    @RequestMapping
    @PermissionLimit(adminuser = true)
    public String index(Model model) {
        model.addAttribute("belongs", DbinfoBelongEnum.values());//数据链接归属

        return "dbinfo/dbinfo.index";
    }

    @RequestMapping("/pageList")
    @ResponseBody
    @PermissionLimit(adminuser = true)
    public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,
                                        @RequestParam(required = false, defaultValue = "10") int length,
                                        String name, String belong, String remark, String filterTime) {
        // parse param
        Date triggerTimeStart = null;
        Date triggerTimeEnd = null;
        if (filterTime!=null && filterTime.trim().length()>0) {
            String[] temp = filterTime.split(" - ");
            if (temp!=null && temp.length == 2) {
                triggerTimeStart = DateUtil.parseDateTime(temp[0]);
                triggerTimeEnd = DateUtil.parseDateTime(temp[1]);
            }
        }

        // page list
        List<DbInfo> list = dbInfoDao.pageList(start, length, name, belong, remark, null, null);
        int list_count = dbInfoDao.pageListCount(start, length, name, belong, remark, null, null);

        System.out.println(list);
        // package result
        Map<String, Object> maps = new HashMap<>();
        maps.put("recordsTotal", list_count);		// 总记录数
        maps.put("recordsFiltered", list_count);	// 过滤后的总记录数
        maps.put("data", list);  					// 分页列表
        return maps;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ReturnT<String> add(DbInfo dbInfo) {
        return dbInfoService.add(dbInfo);
    }

    @RequestMapping("/detail")
    @ResponseBody
    public ReturnT<DbInfo> detail(Long id) {
        return dbInfoService.loadById(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public ReturnT<String> update(DbInfo dbInfo) {
        return dbInfoService.update(dbInfo);
    }

    @RequestMapping("/copy")
    @ResponseBody
    public ReturnT<String> copy(Long id) {
        return dbInfoService.copy(id);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ReturnT<String> delete(Long id) {
        return dbInfoService.remove(id);
    }

    @RequestMapping("/testDbInfoById")
    @ResponseBody
    public ReturnT<String> testDbInfoById(Long id) {
        return dbInfoService.testDbInfoById(id);
    }

    @RequestMapping("/testNewDbInfo")
    @ResponseBody
    public ReturnT<String> testNewDbInfo(DbInfo dbInfo) {
        return dbInfoService.testNewDbInfo(dbInfo);
    }
}
